package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.itheima.dto.OrderDTO;
import com.itheima.pojo.Member;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.MemberService;
import com.itheima.service.OrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.OrderMapper;
import com.itheima.pojo.Order;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.DateUtil;
import com.itheima.utils.RedisUtil;
import com.itheima.vo.OrderVO;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

@DubboService
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Override
    public OrderDTO successOrder(Long orderId) {
        return baseMapper.successOrder(orderId);
    }


    @Resource
    private MemberService memberService;

    @Resource
    private OrderSettingService orderSettingService;

    @Override
    @Transactional
    public Long add(OrderVO orderVO) {

        //短信验证码校验
        /**
         * 先看一看redis是否有发送的验证码
         */
        String code = RedisUtil.get("sms:" + orderVO.getTelephone());
        if (ObjectUtils.isEmpty(code)){
            throw new RuntimeException("您没有发送验证码或验证码已失效");
        }
        /**
         * 走到这则证明有发送验证码，在判断它输入的验证码和发送的验证码是否一致
         */
        if (code.equals(orderVO.getValidateCode())==false){
            throw  new RuntimeException("您输入的验证码有误,请重新输入");
        }
        String orderDate = DateUtil.formatDate(orderVO.getOrderDate());
        //判断是否重复预约
        /**
         * 先根据手机号查找会员
         * 如果会员不为空
         */
        Member member = memberService.findByTelephone(orderVO.getTelephone());
        if (member!=null){
            /**
             * 进入则手机号已经注册，在看是否预约。
             */
            Order order=existss(member.getId(),orderDate);
            if (order!=null){
                throw new RuntimeException("抱歉，不可以重复预约。");
            }
        }
        //当前日期是否可以预约
        OrderSetting orderSetting = orderSettingService.findByDate(orderDate);
        if (orderSetting==null){
            throw new RuntimeException("今天无预约，请改天在约。");
        }
        if (orderSetting.getReservations().intValue()==orderSetting.getNumber().intValue()){
            throw new RuntimeException("当前预约已满，请改天在约。");
        }

        return doAdd(orderSetting,member,orderVO);
    }

    private Long doAdd(OrderSetting orderSetting,Member member,OrderVO orderVO){
        //预约设置预约数修改+1
        orderSetting.setReservations(orderSetting.getReservations()+1);
        orderSettingService.updateById(orderSetting);
        //会员信息维护
        //如果为空则为首次注册
        if (member==null){
            member = new Member();
            member.setCreateDate(new Date());
        }
        BeanUtils.copyProperties(orderVO,member);
        memberService.saveOrUpdate(member);
        //预定订单
        Order order = new Order();
        order.setMemberId(member.getId());
        order.setOrderDate(orderVO.getOrderDate());
        order.setSetmealId(orderVO.getSetmealId());
        this.save(order);
        return order.getId();

    }

    @Override
    public Order existss(Long memberId, String orderDate) {
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Order::getId,memberId)
                .eq(Order::getOrderDate,orderDate);
        return this.getOne(queryWrapper);
    }


}